package labs.laba_03;

import java.math.BigDecimal;
import java.util.ArrayList;

import android.graphics.Canvas;
import android.os.Debug;
import android.util.Log;
import labs.laba_03.FigureTypes;

abstract public class Figure {
	protected Dot center;
	protected float width;
	protected float height;
	private Figure parent;
	private boolean isRoot;
	final private FigureTypes type;
	private ArrayList<Figure> childList;
	private Figure mChild = null;

	protected Figure(float center_x, float center_y, Figure parent, FigureTypes ft, boolean itRoot){
		this.isRoot = itRoot;
		//
		this.center = new Dot(center_x, center_y);
		this.parent = parent;
		this.type = ft;
		//
		if(!isRoot){
			this.width = BigDecimal.valueOf((parent.getWidth()/100)*FiguresEngine.DEFAULT_PERCENT).setScale(2).floatValue();
			if(this.width<FiguresEngine.MIN_WIDTH) this.width = FiguresEngine.MIN_WIDTH; 
			this.height = this.width;
		}
		//
		this.childList = new ArrayList<Figure>();
	}
	public Figure(float center_x, float center_y, Figure parent, FigureTypes ft){
		this(center_x, center_y, parent, ft, false);
	}
	public void setHeight(float height) {
		this.height = height;
	}
	public float getHeight() {
		return height;
	}
	public void setWidth(float width) {
		this.width = width;
	}
	public float getWidth() {
		return width;
	}
	public FigureTypes getType(){
		return type;
	}
	public Figure getParent() {
		return parent;
	}
	public Dot getCenter() {
		return center;
	}
	protected void setCenter(Dot center) {
		this.center = center;
	}
	public boolean existActiveChild(){
		if(mChild!=null) return true;
		return false;
	}
	private void setActiveChild(Figure foundChild){
		this.mChild = foundChild;
	}
	public boolean setActiveChildIn(float mScrX, float mScrY){
		Figure foundChild = null;
		Dot dt = new Dot(mScrX, mScrY);
		for(Figure child : childList){
			if(dt.getDistanceTo(child.getCenter())<child.getOuterRadius()){
				foundChild = child;
				child.setActiveChildIn(mScrX, mScrY);
				break;
			}
		}
		setActiveChild(foundChild);
		return foundChild != null;
	}
	public void unsetActiveChildIn(){
		for(Figure child : childList){
			child.unsetActiveChildIn();
		}
		this.mChild = null;
	}
	public boolean moveActiveChildIn(float mScrX, float mScrY){
		if(this.mChild==null) return false;
		try{
			//Figure tempCh = (Figure)mChild.clone();
			Dot precCent = mChild.getCenter();
			mChild.setCenter(new Dot(mScrX, mScrY));
			if(canBeMovedHere(mChild))
				return true;
			else
				mChild.setCenter(precCent);
		}catch (Exception e) {
			Log.d("My_Error", e.getMessage());
		}
		return false;
	}
	private boolean canBeMovedHere(Figure nf){
		for(Figure child : childList){
			if(!child.equals(mChild) && child.near(nf))
				if(FiguresEngine.intersection(nf, child)) return false;
		}
		return true;
	}
	public boolean addChild(Figure nf){
		if(canBeMovedHere(nf)){
			childList.add(nf);
			return true;
		}
		return false;
	}
	public ArrayList<Figure> getChildList(){
		return childList;
	}
	private boolean near(Figure f){
		if(this.center.getDistanceTo(f.getCenter()) > (f.getOuterRadius()+this.getOuterRadius()))
			return false;
		return true;
	}
	abstract public float getOuterRadius();
	abstract public Object getPrimitive();
	abstract public void draw(Canvas canvas);
	protected void drawHierarchy(Canvas canvas){
		float dx,dy = 1;
		for(Figure child: childList){
			child.draw(canvas);
		}
	}
}
